1
|
|
|
var ExtensibleData = require('./ExtensibleData'), |
2
|
|
|
utils = require('./utils'); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* An address. |
6
|
|
|
* |
7
|
|
|
* @constructor |
8
|
|
|
* @param {Object} [json] |
|
|
|
|
9
|
|
|
*/ |
10
|
|
|
var Address = function(json){ |
11
|
|
|
|
12
|
|
|
// Protect against forgetting the new keyword when calling the constructor |
13
|
|
|
if(!(this instanceof Address)){ |
14
|
|
|
return new Address(json); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
// If the given object is already an instance then just return it. DON'T copy it. |
18
|
|
|
if(Address.isInstance(json)){ |
19
|
|
|
return json; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
ExtensibleData.call(this, json); |
23
|
|
|
|
24
|
|
|
if(json){ |
|
|
|
|
25
|
|
|
this.setValue(json.value); |
26
|
|
|
this.setCity(json.city); |
27
|
|
|
this.setCountry(json.country); |
28
|
|
|
this.setPostalCode(json.postalCode); |
29
|
|
|
this.setStateOrProvince(json.stateOrProvince); |
30
|
|
|
this.setStreet(json.street); |
31
|
|
|
this.setStreet2(json.street2); |
32
|
|
|
this.setStreet3(json.street3); |
33
|
|
|
this.setStreet4(json.street4); |
34
|
|
|
this.setStreet5(json.street5); |
35
|
|
|
this.setStreet6(json.street6); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
}; |
38
|
|
|
|
39
|
|
|
Address.prototype = Object.create(ExtensibleData.prototype); |
40
|
|
|
|
41
|
|
|
Address._gedxClass = Address.prototype._gedxClass = 'GedcomX.Address'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Check whether the given object is an instance of this class. |
45
|
|
|
* |
46
|
|
|
* @param {Object} obj |
47
|
|
|
* @returns {Boolean} |
48
|
|
|
*/ |
49
|
|
|
Address.isInstance = function(obj){ |
50
|
|
|
return utils.isInstance(obj, this._gedxClass); |
51
|
|
|
}; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get the complete address value |
55
|
|
|
* |
56
|
|
|
* @returns {String} |
57
|
|
|
*/ |
58
|
|
|
Address.prototype.getValue = function(){ |
59
|
|
|
return this.value; |
60
|
|
|
}; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set the complete address value |
64
|
|
|
* |
65
|
|
|
* @param {String} value |
66
|
|
|
* @return {Address} |
67
|
|
|
*/ |
68
|
|
|
Address.prototype.setValue = function(value){ |
69
|
|
|
this.value = value; |
70
|
|
|
return this; |
71
|
|
|
}; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get the city |
75
|
|
|
* |
76
|
|
|
* @returns {String} |
77
|
|
|
*/ |
78
|
|
|
Address.prototype.getCity = function(){ |
79
|
|
|
return this.city; |
80
|
|
|
}; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Set the city |
84
|
|
|
* |
85
|
|
|
* @param {String} city |
86
|
|
|
* @returns {Address} |
87
|
|
|
*/ |
88
|
|
|
Address.prototype.setCity = function(city){ |
89
|
|
|
this.city = city; |
90
|
|
|
return this; |
91
|
|
|
}; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get the country |
95
|
|
|
* |
96
|
|
|
* @returns {String} |
97
|
|
|
*/ |
98
|
|
|
Address.prototype.getCountry = function(){ |
99
|
|
|
return this.country; |
100
|
|
|
}; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Set the country |
104
|
|
|
* |
105
|
|
|
* @param {String} country |
106
|
|
|
* @returns {Address} |
107
|
|
|
*/ |
108
|
|
|
Address.prototype.setCountry = function(country){ |
109
|
|
|
this.country = country; |
110
|
|
|
return this; |
111
|
|
|
}; |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get the postal code |
115
|
|
|
* |
116
|
|
|
* @returns {String} |
117
|
|
|
*/ |
118
|
|
|
Address.prototype.getPostalCode = function(){ |
119
|
|
|
return this.postalCode; |
120
|
|
|
}; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Set the postal code |
124
|
|
|
* |
125
|
|
|
* @param {String} postalCode |
126
|
|
|
* @returns {Address} |
127
|
|
|
*/ |
128
|
|
|
Address.prototype.setPostalCode = function(postalCode){ |
129
|
|
|
this.postalCode = postalCode; |
130
|
|
|
return this; |
131
|
|
|
}; |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get the state or province |
135
|
|
|
* |
136
|
|
|
* @returns {String} |
137
|
|
|
*/ |
138
|
|
|
Address.prototype.getStateOrProvince = function(){ |
139
|
|
|
return this.stateOrProvince; |
140
|
|
|
}; |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Set the state or province |
144
|
|
|
* |
145
|
|
|
* @param {String} stateOrProvince |
146
|
|
|
* @returns {Address} |
147
|
|
|
*/ |
148
|
|
|
Address.prototype.setStateOrProvince = function(stateOrProvince){ |
149
|
|
|
this.stateOrProvince = stateOrProvince; |
150
|
|
|
return this; |
151
|
|
|
}; |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get the street |
155
|
|
|
* |
156
|
|
|
* @returns {String} |
157
|
|
|
*/ |
158
|
|
|
Address.prototype.getStreet = function(){ |
159
|
|
|
return this.street; |
160
|
|
|
}; |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Set the street |
164
|
|
|
* |
165
|
|
|
* @param {String} street |
166
|
|
|
* @returns {Address} |
167
|
|
|
*/ |
168
|
|
|
Address.prototype.setStreet = function(street){ |
169
|
|
|
this.street = street; |
170
|
|
|
return this; |
171
|
|
|
}; |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get the street2 |
175
|
|
|
* |
176
|
|
|
* @returns {String} |
177
|
|
|
*/ |
178
|
|
|
Address.prototype.getStreet2 = function(){ |
179
|
|
|
return this.street2; |
180
|
|
|
}; |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Set the street2 |
184
|
|
|
* |
185
|
|
|
* @param {String} street2 |
186
|
|
|
* @returns {Address} |
187
|
|
|
*/ |
188
|
|
|
Address.prototype.setStreet2 = function(street2){ |
189
|
|
|
this.street2 = street2; |
190
|
|
|
return this; |
191
|
|
|
}; |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Get the street3 |
195
|
|
|
* |
196
|
|
|
* @returns {String} |
197
|
|
|
*/ |
198
|
|
|
Address.prototype.getStreet3 = function(){ |
199
|
|
|
return this.street3; |
200
|
|
|
}; |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Set the street3 |
204
|
|
|
* |
205
|
|
|
* @param {String} street3 |
206
|
|
|
* @returns {Address} |
207
|
|
|
*/ |
208
|
|
|
Address.prototype.setStreet3 = function(street3){ |
209
|
|
|
this.street3 = street3; |
210
|
|
|
return this; |
211
|
|
|
}; |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Get the street4 |
215
|
|
|
* |
216
|
|
|
* @returns {String} |
217
|
|
|
*/ |
218
|
|
|
Address.prototype.getStreet4 = function(){ |
219
|
|
|
return this.street4; |
220
|
|
|
}; |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Set the street4 |
224
|
|
|
* |
225
|
|
|
* @param {String} street4 |
226
|
|
|
* @returns {Address} |
227
|
|
|
*/ |
228
|
|
|
Address.prototype.setStreet4 = function(street4){ |
229
|
|
|
this.street4 = street4; |
230
|
|
|
return this; |
231
|
|
|
}; |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Get the street5 |
235
|
|
|
* |
236
|
|
|
* @returns {String} |
237
|
|
|
*/ |
238
|
|
|
Address.prototype.getStreet5 = function(){ |
239
|
|
|
return this.street5; |
240
|
|
|
}; |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Set the street5 |
244
|
|
|
* |
245
|
|
|
* @param {String} street5 |
246
|
|
|
* @returns {Address} |
247
|
|
|
*/ |
248
|
|
|
Address.prototype.setStreet5 = function(street5){ |
249
|
|
|
this.street5 = street5; |
250
|
|
|
return this; |
251
|
|
|
}; |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Get the street6 |
255
|
|
|
* |
256
|
|
|
* @returns {String} |
257
|
|
|
*/ |
258
|
|
|
Address.prototype.getStreet6 = function(){ |
259
|
|
|
return this.street6; |
260
|
|
|
}; |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Set the street6 |
264
|
|
|
* |
265
|
|
|
* @param {String} street6 |
266
|
|
|
* @returns {Address} |
267
|
|
|
*/ |
268
|
|
|
Address.prototype.setStreet6 = function(street6){ |
269
|
|
|
this.street6 = street6; |
270
|
|
|
return this; |
271
|
|
|
}; |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Export the object as JSON |
275
|
|
|
* |
276
|
|
|
* @return {Object} JSON object |
277
|
|
|
*/ |
278
|
|
|
Address.prototype.toJSON = function(){ |
279
|
|
|
var json = ExtensibleData.prototype.toJSON.call(this); |
280
|
|
|
|
281
|
|
|
if(this.value){ |
282
|
|
|
json.value = this.value; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
if(this.city){ |
286
|
|
|
json.city = this.city; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
if(this.country){ |
290
|
|
|
json.country = this.country; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
if(this.postalCode){ |
294
|
|
|
json.postalCode = this.postalCode; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
if(this.stateOrProvince){ |
298
|
|
|
json.stateOrProvince = this.stateOrProvince; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
if(this.street){ |
302
|
|
|
json.street = this.street; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
if(this.street2){ |
306
|
|
|
json.street2 = this.street2; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
if(this.street3){ |
310
|
|
|
json.street3 = this.street3; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
if(this.street4){ |
314
|
|
|
json.street4 = this.street4; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
if(this.street5){ |
318
|
|
|
json.street5 = this.street5; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
if(this.street6){ |
322
|
|
|
json.street6 = this.street6; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
return json; |
326
|
|
|
}; |
327
|
|
|
|
328
|
|
|
module.exports = Address; |